package com.example.a2032.activity;

import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a2032.R;
import com.example.a2032.Utils.SharedPreferenceUtils;
import com.example.a2032.Utils.Utils;

import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BrightSettingActivity extends BaseActivity {

    @BindView(R.id.iv_title)
    ImageView ivTitle;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_bright_icon)
    ImageView ivBrightIcon;

    @BindView(R.id.iv_setting_reduce)
    ImageView ivSettingReduce;
    @BindView(R.id.iv_setting_add)
    ImageView ivSettingAdd;
    @BindViews({R.id.iv_range0,R.id.iv_range1,R.id.iv_range2})
    List<ImageView> listViews;
    private int brightLevel;
    Integer[]brightLevels=new Integer[]{50,125,255};
    private static final String TAG = "BrightSettingActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bright_setting);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        brightLevel=getScreenBrightness();
        brightLevel= Arrays.asList(brightLevels).indexOf(brightLevel);
        Log.i(TAG, "init: "+brightLevel);
        String title = getIntent().getStringExtra(SettingActivity.KEY_SettingTitle);
        tvTitle.setText(title);
       // brightLevel= SharedPreferenceUtils.getInteger(SharedPreferenceUtils.KEY_BrightLevel,2);
        showBrightLevel(brightLevel);
    }
      int getScreenBrightness() {
        int value = 0;
        ContentResolver cr = getContentResolver();
        try {
            value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {

        }
        return value;
    }

    @OnClick(R.id.iv_title)
    public void onViewClicked() {
        finish();
    }

    @OnClick({R.id.iv_title, R.id.iv_setting_reduce, R.id.iv_setting_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title:
                finish();
                break;
            case R.id.iv_setting_reduce:
                reduce();
                break;
            case R.id.iv_setting_add:
                add();
                break;
        }
    }
    private void showBrightLevel(int brightLevel){
        if(brightLevel==-1)brightLevel=0;
        for(ImageView imageView:listViews){
            imageView.setImageResource(R.mipmap.setting_range_unchecked);
        }
        for(int i=0;i<brightLevel+1;i++){
            listViews.get(i).setImageResource(R.mipmap.setting_range_checked);
        }
        Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,brightLevels[brightLevel]);
    }
    private void add(){
        if(brightLevel<2)brightLevel++;
         changeBright(brightLevel);
    }
    private void reduce(){
        if(brightLevel>0)brightLevel--;
        changeBright(brightLevel);
    }
    private void changeBright(int brightLevel){
        showBrightLevel(brightLevel);
       // SharedPreferenceUtils.setSpInteger(SharedPreferenceUtils.KEY_BrightLevel,brightLevel);
    }

}